<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">


			<template #content>
				<view class="content" style="height: 100%;">
					<view class="contentBox">
						<view class="container">
							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">

									<uni-forms-item label="保养编号:" label-width="auto">
										<input v-model="baseFormData.BillNO" type="text" placeholder="自动带出保养编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="机台:" label-width="auto">
										<input v-model="baseFormData.EquipmentName" type="text" placeholder="机台"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="保养人员:" label-width="auto">
										<input v-model="baseFormData.UpkeepMan" type="text" disabled />
									</uni-forms-item>
									<uni-forms-item label="开始时间:" label-width="auto">
										<input v-model="baseFormData.StartMaintenance" type="text"
											placeholder="自动带出开始时间" disabled />
									</uni-forms-item>
									<!-- EndMaintenance -->
									<uni-forms-item label="结束时间:" label-width="auto">
										<input v-model="baseFormData.EndMaintenance" type="text"
											placeholder="自动带出结束时间" disabled />
									</uni-forms-item>
									<uni-forms-item label="保养周期:" label-width="auto">
										<input v-model="baseFormData.PeriodTypeName" type="text" placeholder="请输入保养周期"
											disabled />
										<text class="text" @click="PeriodTypeShow=true"></text>
									</uni-forms-item>

								</view>
							</uni-forms>
						</view>
						<view class="table">
							<table>
								<tr>
									<th width="10%">序号</th>
									<th>保养项目</th>
									<th width='25%'>保养结果</th>
								</tr>
								<tr v-for="(item, index) in tableData">

									<td>{{index + 1}}</td>
									<td>{{item.UpkeepItemContent}}</td>
									<td>
										<uni-data-checkbox v-model="item.FResult" :localdata="sexs" />
									</td>
								</tr>

							</table>

						</view>
					</view>


					<view class="bottomBtnsOne">
						<button block @click="submit" v-show="submitButtonShow">
							{{submitButton}}
						</button>
					</view>
					<u-action-sheet :actions="PeriodTypeList" :show="PeriodTypeShow" @select="PeriodTypeClick"
						@close="PeriodTypeShow=false"></u-action-sheet>
					<u-toast ref="uToast"></u-toast>
					<u-datetime-picker :show="Timeshow" v-model="value1" mode="date" @cancel='Timeshow=false'
						@confirm='pickerConfirm'></u-datetime-picker>

				</view>
			</template>

		</mc-framework>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime
	} from '@/mes/common/base.js';
	export default {
		data() {
			return {
				baseFormData: {
					EquipmentName: '',
					EquipmentID: '',
					PeriodTypeName: '周',
					PeriodType: 'W',
					UpkeepMan: '',
					BillDate: '',
				},
				sexs: [{
					text: '合格',
					value: 0
				}, {
					text: '不合格',
					value: 1
				}],
				PeriodTypeShow: false,
				PeriodTypeList: [{
						name: '月',
						value: 'M'
					},
					{
						name: '周',
						value: 'W'
					},

					{
						name: '半年',
						value: 'H'
					},
				],
				tableData: [],
				Timeshow: false,
				value1: Number(new Date()),
				submitButton: '',
				submitButtonShow: true
			};
		},
		onLoad(item) {
			var that = this
			console.log(item);
			if (item.UpkeepEquipmentID) {
				// that.submitButton = '结束保养'
				that.queryList(item.UpkeepEquipmentID)
			} else {

				that.submitButton = '开始保养'
				this.baseFormData.UpkeepMan = this.baseFormData.UpkeepMan ? this.baseFormData.UpkeepMan : this
					.$store.state.UserInfo.UserName
				this.baseFormData.CheckupID = this.baseFormData.CheckupID ? this.baseFormData.CheckupID : this.$store
					.state.UserInfo.EmplID
				this.baseFormData.EquipmentName = this.baseFormData.EquipmentName ? this.baseFormData.EquipmentName : this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentName
				this.baseFormData.EquipmentID = this.baseFormData.EquipmentID ? this.baseFormData.EquipmentID : this
					.$store.state.GuyueModule.MesBasicInfo.warehouse.EquipmentID
				this.baseFormData.BillDate = this.baseFormData.BillDate ? this.baseFormData.BillDate :
					getFormatDateTime(new Date())
				that.addPeriodTypeClick()
			}
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			pickerConfirm(e) {
				var time = getFormatDateTime(e.value)
				this.baseFormData.BillDate = time
				this.Timeshow = false
				this.$forceUpdate()
			},
			addPeriodTypeClick() {
				var that = this
				if (that.baseFormData.PeriodType == 'H') {
					that.baseFormData.PeriodTypeName = '半年'
				} else if (that.baseFormData.PeriodType == 'M') {
					that.baseFormData.PeriodTypeName = '月'
				} else {
					that.baseFormData.PeriodTypeName = '周'
				}
				var obj = {
					value: that.baseFormData.PeriodType,
					name: that.baseFormData.PeriodTypeName
				}
				that.PeriodTypeClick(obj)
			},
			PeriodTypeClick(e) {
				var that = this

				that.baseFormData.PeriodType = e.value
				that.baseFormData.PeriodTypeName = e.name
				that.PeriodTypeShow = false
				that.Request.request('getCheckupItem', {
					PeriodType: e.value,
					FType: 'SBBY'
				}, (ret) => {

					ret.data.ds.map(item => item.FResult = item.FResult ? item.FResult : 0)
					ret.data.ds.map(item => item.UpkeepEquipmentDeID = item.UpkeepEquipmentDeID ? item
						.UpkeepEquipmentDeID : 0)

					that.tableData = ret.data.ds
					that.$forceUpdate()
				})
			},

			submit() {
				var that = this
				var show = true
	
				if (show) {
					var obj = {
						UpkeepMan: that.baseFormData.UpkeepMan,
						EquipmentID: that.baseFormData.EquipmentID,
						UpkeepEquipmentID: that.baseFormData.UpkeepEquipmentID ? that.baseFormData
							.UpkeepEquipmentID : 0,
						PeriodType: that.baseFormData.PeriodType,
						UpID: that.tableData.map(item => item.UpID).join(','),
						UpkeepItemID: that.tableData.map(item => item.UpkeepItemID).join(','),
						FResult: that.tableData.map(item => item.FResult).join(','),
						BillDate: that.baseFormData.BillDate,
						UpkeepItemContent: that.tableData.map(item => item.UpkeepItemContent).join(','),
						UpkeepEquipmentDeID: that.tableData.map(item => item.UpkeepEquipmentDeID).join(','),
						FType: 'SBBY',
					}
					console.log(obj);
					that.Request.request('saveUpkeepEquipment', obj, function(ret) {
						console.log(ret);
						if (ret.status == 200) {
							that.$refs.uToast.show({
								title: '保存',
								type: 'success',
								message: '保存成功'
							})
							that.queryList(ret.data.ds[0].UpkeepEquipmentID)
						}
					})
				}
			},
			queryList(id) {
				var that = this
				try {
					that.Request.request('getUpKeepEquipmentDetail', {
						UpkeepEquipmentID: id
					}, (ret) => {
						console.log(ret.data);
	
						that.baseFormData = ret.data.ds[0]
						that.baseFormData.StartMaintenance = that.baseFormData.StartMaintenance ? that.baseFormData
							.StartMaintenance.replace('T', ' ') : ''
						that.baseFormData.EndMaintenance = that.baseFormData.EndMaintenance ? that.baseFormData
							.EndMaintenance.replace('T', ' ') : ''
						that.tableData = ret.data.ds1
						that.$forceUpdate()
						if (that.baseFormData.BYStatus == 0) {
							that.submitButton = '开始保养'
						} else if (that.baseFormData.BYStatus == 1) {
							that.submitButton = '结束保养'
						} else {
							
							that.submitButtonShow = false
						}
					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},
			changeLevel(data, row) {
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - row.length; // 计算返回页面栈数

				if (backIndex == 1) {
					let prevPage = routes[routes.length - 2]
					prevPage.$vm.getList()
				}
				uni.navigateBack({
					delta: backIndex
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";
</style>